package Game;

import java.awt.*;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

public class Obj {

    Image img;//图片
    int x,y;//坐标
    int w=30,h=30;//宽高
    Game.Window frame;//建立桌面

    public Obj() {
    }
    public Obj(Image img, int x, int y, Game.Window frame) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.frame = frame;
    }
    public Obj(int x, Image img, int y, int w, int h, Game.Window frame) {
        this.x = x;
        this.img = img;
        this.y = y;
        this.w = w;
        this.h = h;
        this.frame = frame;
    }

    public Image getImg() {
        return img;
    }
    public void setImg(Image img) {
        this.img = img;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getW() {
        return w;
    }
    public void setW(int w) {
        this.w = w;
    }
    public int getH() {
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    public java.awt.Window getFrame() {
        return frame;
    }
    public void setFrame(Game.Window frame) {
        this.frame = frame;
    }

    public void paintSelf(Graphics g) {//绘制自身
        g.drawImage(img,x,y,null);
    }
}

  class Head extends Obj {

    private String direction="right";

    public Head(Image img, int x, int y, Game.Window frame) {
        super(img, x, y, frame);
        this.frame.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    public void changeDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                if(!"right".equals(direction)){
                    direction="left";
                    img=Resource.head_l;
                    break;
                }
            case KeyEvent.VK_D:
                if(!"left".equals(direction)) {
                    direction = "right";
                    img = Resource.head_r;
                    break;
                }
            case KeyEvent.VK_W:
                if(!"down".equals(direction)) {
                    direction = "up";
                    img = Resource.head_u;
                    break;
                }
            case KeyEvent.VK_S:
                if(!"up".equals(direction)) {
                    direction = "down";
                    img = Resource.head_d;
                    break;
                }
            default:
                break;
        }
    }

    public String getDirection() {
        return direction;
    }
    public void setDirection(String direction) {
        this.direction = direction;
    }

      public void move (){
        java.util.List<Body> bodyList=this.frame.bodyList;
        for(int i=bodyList.size()-1;i>=1;i--){
            bodyList.get(i).x=bodyList.get(i-1).x;
            bodyList.get(i).y=bodyList.get(i-1).y;

            if(this.x==bodyList.get(i).x&&this.y==bodyList.get(i).y){
                Game.Window.state=3;
            }
        }

          bodyList.get(0).x=this.x;
          bodyList.get(0).y=this.y;

          switch (direction){//蛇头移动
              case "up":
                  y-=h;
                  break;
              case "down":
                  y+=h;
                  break;
              case "left":
                  x-=w;
                  break;
              case "right":
                  x+=w;
                  break;
          }
      }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);

        Integer newx=null;//身体最后一节的坐标
        Integer newy=null;//身体最后一节的坐标

        Food food1=this.frame.food1;
        Food food2=this.frame.food2;
        Food food3=this.frame.food3;

        if(Math.abs(this.x-food1.x)<8&&Math.abs(this.y-food1.y)<8){//蛇吃食物
            this.frame.food1=food1.getFood();
            Body lastbody=this.frame.bodyList.get(this.frame.bodyList.size()-1);
            newx=lastbody.x;
            newy=lastbody.y;
            Game.Window.score++;
        }
        if(Math.abs(this.x-food2.x)<8&&Math.abs(this.y-food2.y)<8&&Resource.level>=4){//蛇吃食物
            this.frame.food2=food2.getFood();
            Body lastbody=this.frame.bodyList.get(this.frame.bodyList.size()-1);
            newx=lastbody.x;
            newy=lastbody.y;
            Game.Window.score++;
        }
        if(Math.abs(this.x-food3.x)<8&&Math.abs(this.y-food3.y)<8&&Resource.level>=4){//蛇吃食物
            this.frame.food3=food3.getFood();
            Body lastbody=this.frame.bodyList.get(this.frame.bodyList.size()-1);
            newx=lastbody.x;
            newy=lastbody.y;
            Game.Window.score++;
        }

        if(Resource.level!=5) {
            if (Game.Window.score >= 1) {
                Game.Window.state = 4;
            }
        }

        move();

        if(newx!=null&&newy!=null){
            this.frame.bodyList.add(new Body(Resource.body,newx,newy,this.frame));
        }

        //越界处理
        if(x<0){
            x=885;
        }else if(x>885){
            x=15;
        }else if(y<30){
            y=878;
        }else if(y>900){
            y=8;
        }
    }
}

  class Body extends Obj{
      public Body(Image img, int x, int y, Game.Window frame) {
          super(img, x, y, frame);
      }

      @Override
      public void paintSelf(Graphics g) {
          super.paintSelf(g);
      }
  }

  class Food extends Obj{

    Random rand=new Random();
      public Food() {
          super();
      }
      public Food(Image img, int x, int y, Game.Window frame) {
          super(img, x, y, frame);
      }

      public Food getFood(){//食物随机生产
          return new Food(Resource.food,(rand.nextInt(30)*30)-15,(rand.nextInt(29)*30)+8,this.frame);
      }

      @Override
      public void paintSelf(Graphics g) {
          super.paintSelf(g);
      }
  }